<%@page import="java.util.*"%>
<%@page import="model.*"%>
<%@page import="model.environment.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<% { // New scope, so variables here doesn't conflict with the pages that include the header.%>
<% 
// Developer line. Remove when commiting.  
// PageViewInfo pvi = new PageViewInfo();
String title = pvi.getPageTitle(); 

/*
 * Handle login post data.
 */
int loginStatus = 0; // 0: No errors, 1: Login error, 2: Create user error. 
Object sessionMessage = session.getAttribute("pviMessage");
if (sessionMessage == StaticInfo.Message.LOGIN_OK)
{
	pvi.setStatusMessage("Succesfull Login");
	session.setAttribute("pviMessage", null);
}
else if (sessionMessage == StaticInfo.Message.LOGIN_FAILED)
{
	pvi.setStatusMessage("Failed to login"); 
	session.setAttribute("pviMessage", null);
	loginStatus = 1;
}
else if (sessionMessage == StaticInfo.Message.CREATEUSER_OK)
{
	pvi.setStatusMessage("Succesfully created a user");
	session.setAttribute("pviMessage", null);
}
else if (sessionMessage == StaticInfo.Message.CREATEUSER_FAILED)
{
	pvi.setStatusMessage("Error creating a user");
	session.setAttribute("pviMessage", null);
	loginStatus = 2;
}
String message = pvi.getStatusMessage();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="http://webbies.dk/apocalypse/css/jquery-ui-1.8.18.custom.css" />
<link rel="stylesheet" type="text/css" href="http://webbies.dk/apocalypse/css/style.css" />
<link type='text/css' href='http://webbies.dk/apocalypse/css/basic.css' rel='stylesheet' media='screen' />
<!-- IE6 "fix" for the close png image -->
<!--[if lt IE 7]><link type='text/css' href='http://webbies.dk/apocalypse/css/basic_ie.css' rel='stylesheet' media='screen' /><![endif]-->
<title>Apocalypse store <% if (!title.equals("")) {out.println(" - " + title);} %></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://webbies.dk/apocalypse/js/jquery-ui-1.8.18.custom.min.js"></script>
<script type='text/javascript' src='http://webbies.dk/apocalypse/js/jquery.simplemodal.js'></script>
<script type='text/javascript' src='http://webbies.dk/apocalypse/js/jquery.properload.min.js'></script>
<script type="text/javascript" src="http://webbies.dk/apocalypse/js/jquery.sudoSlider.js"></script>
<!-- <script type="text/javascript" src="http://webbies.dk/apocalypse/js/jquery.infieldlabel.js"></script> Just using plain old HTML5 instead (and still make it usefull for the browsers that doesn't support HTML5 -->
<!-- <script type="text/javascript" src="http://webbies.dk/apocalypse/js/jquery.touchSwipe-1.2.5.js"></script> -->
<script type="text/javascript" src="http://webbies.dk/apocalypse/js/script.js"></script>
<% if (loginStatus != 0)
{%>
<script type="text/javascript">
	$(document).ready(function(){
		$('#loginKnap').click();
		<% if (loginStatus == 2)
		{%>
			$(".newUserRadioButton").trigger("click");
			$("form.loginForm").attr("action", "createUser.do");
			$(".loginFormRepeatPass").show(0);
		<% } %>
	});
</script>
<% } %>
</head>
<body>
    <div class="header">
    	<% if (message != null && !message.equals("")) 
    	{ %>
    	<div class="message">
        	<div class="messageTestBox" style="margin:7px 30px;">
            	<%= message %>
            </div>
        </div>
        <% } %>
    	<h1 class="headline">Apocalypse store</h1>
		<div class="menu">
            <ul id="navigationMenu">
            	<% 
            	title = title.toLowerCase();
            	String homeclass = title.equals("front page") ? "selectedMenu" : "normalMenu";
            	String productsclass = title.equals("products") ? "selectedMenu" : "normalMenu";
            	String basketclass = title.equals("basket") ? "selectedMenu" : "normalMenu";
            	String adminclass = title.contains("Administration") ? "selectedMenu" : "normalMenu";
            	
            	String contextPath = request.getServletContext().getContextPath();
            	%>
                <li><a href="<%= contextPath %>/frontpage.jsp" class="<%= homeclass %>">Home</a></li>
                <li><a href="<%= contextPath %>/showAllItems.jsp" class="<%= productsclass %>">Products</a></li> 
                <li><a href="<%= contextPath %>/basket.jsp" class="<%= basketclass %>">Basket</a></li>
                <li id="otherShopsKnap"><a href="#" class="normalMenu">Other stores</a></li>
                <div id="otherShops">
                   	<h1>Other shops in the network:</h1>
                    <ul>
                    <%
                   		List<Shop> shops = Shops.getShops();
						for (Shop s : shops) {
							out.println("<li><a  href=\"" + s.getShopURL() + "\">" + s.getShopName() + "</a></li>");
						} %>
                    </ul>
                </div>
                <% if (Login.isLoggedIn(session)) 
                { 
                	if (Login.isAdministrator(session))
                	{
                		%>
                		<li><a href="<%=contextPath %>/admin/frontpage.jsp" class="<%= adminclass %>">Admin stuff</a></li>
                		
                		<%
                	}
                	%> 
                	<li style="cursor:default;"><a href="<%= contextPath %>/logout.do" class="normalMenu" style="cursor:default;">Log out</a></li> 
                	<%
	            } else { %>
                <li id="loginKnap"><a href="#" class="normalMenu">Login</a></li>
                <div id="loginForm">
                    <form class="loginForm" action="login.do" method="post" accept-charset="utf-8">
                        <fieldset>
                           	<% if (loginStatus == 0)
                           	{ %>
                           		<legend>User login</legend>
                           	<% } else if (loginStatus == 1) { %>
                           		<legend style="font-color:red;">Login failed</legend>
                           	<% }else if (loginStatus == 2) { %>
	                       		<legend style="font-color:red;">Failed to create a user</legend>
	                       	<% } %>
                            <p>
                                <label for="username">Username</label><br />
                                <input type="text" name="username" value="" id="username"  placeholder="Username">
                            </p>
                            <p style="margin-top:10px;margin-bottom:5px;"> <div style="display:block;"><input type="radio" name="new" value="false" checked="checked" class="newUserRadioButton old"> Already have a user. </div></p>
							<p style="margin-bottom:7px;"> <div style="display:block;"><input type="radio" name="new" value="true"  class="newUserRadioButton new"> I need an acount. </div></p>
                            <p>
                                <label for="password">Password</label><br />
                                <input type="password" name="password" value="" id="password1" placeholder="Password">
                            </p>
                            <p style="display:none;" class="loginFormRepeatPass">
                                <label for="password" class="repeatPassLabel">Repeat password</label><br />
                                <input type="password" name="password2" value="" id="password2" placeholder="Repeat">
                            </p>
                        </fieldset>
                        <p><input class="loginSubmit" type="submit" value="Login &rarr;" style="font-size:16px;padding:5px;"></p>
                    </form>
                </div>
                <% } %>
            </ul>
        </div>   
        <div class="basket">
        	<a href="<%= contextPath %>/basket.jsp"><%= ShoppingBasket.getShoppingBasket(session).getItemsInBasket() + " items in basket." %></a>
        </div>
    </div>

<% } %>